define(['jquery','cookie'], () => {
    class Header {
      constructor () {
        this.loadHtml().then(() => {
          this.user()
          this.calCartCount ()
        })
      }
      loadHtml(){
        return new Promise(resolve => {
          $('.header').load('/html/header.html', resolve)
        })
      }
      user(){
        const username=$.cookie('username')
        if($.cookie('username')){
          $('.lg').hide()
          $('.loginD').show()
          $('.user').html(username)
        }
        $('.logindone').on('click',function(){
          if(confirm('你确定退出登陆么?')){
            $('.lg').show()
            $('.loginD').hide()
            $.cookie('username',name,{expires:-1,path:'/'})
          }
        })
      }
      calCartCount () {
        let count = 0
        let cart = localStorage.getItem('cart')
        if (cart) {
          cart = JSON.parse(cart)
          count = cart.reduce((total, shop) => {
            return total + shop.num
          }, 0)
        }
        $('#shopNumber').html(count)
        $('.carGoods .carNum').html(count)
      }
      
    }
    return new Header()
  })